********************************************************
********************************************************
******** THIS FILE GENERATES SUM STATS OF INDICES ******
****************** Tables S.3 and S.4 ******************
********************************************************
********************************************************
*Version: 2022 March 29

cap mkdir `"$output/online_appendix"'
cap mkdir `"$output/online_appendix/index_summary"'

cap log close 

di "This run uses code version from 2023/3/29"

use `"$mcoindices/CIP_indices_medicaid.dta"', clear 
keep payee_state_cd lev_CIP_FE
rename lev_CIP_FE lev_CIP_FE_mco
tempfile mcoindex 
save `mcoindex'

use `"$mover_data/$mover_file"', clear

merge m:1 payee_state_cd using `"$indices/fee_indices_medicaid.dta"', keepusing(pi* mean_*)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `"$indices/CIP_indices_medicaid.dta"', ///
	keepusing(*CIP* mean_denial_rate mean_tau)
keep if _merge == 3
drop _merge 

merge m:1 payee_state_cd using `mcoindex', keepusing(*CIP*)
keep if _merge == 3
drop _merge 


sort npimedicare_id YearPost
unab indexlist:  pi_c_FE_cpt_line_pcp pi_FE_cpt_line pi_c_FE_cpt_line /// 
				 pi_c_W_FE_cpt_line lev_CIP_FE_fv lev_CIP_FE_fve ///
				 lev_CIP_FE_inc lev_CIP_FE_preg lev_CIP_FE lev_CIP_FE_W ///
				 lev_CIP_FE_icd9 lev_CIP_FE_rf lev_CIP_FE_nofe lev_CIP_FE_W_nofe ///
				 lev_CIP_FE_nofe_preg lev_CIP_FE_nofe_fv lev_CIP_FE_nofe_fve ///
				 lev_CIP_FE_nofe_inc lev_CIP_FE_nofe_icd9 lev_CIP_FE_mco ///
				 v_initial_den_CIP CIP_abs CIP_abs_nofe pi_c_FE_cpt_line_abs

unab indexlist2: lev_CIP_FE_fv lev_CIP_FE_fve lev_CIP_FE_inc lev_CIP_FE_preg ///
				 lev_CIP_FE lev_CIP_FE_icd9 lev_CIP_FE_rf lev_CIP_FE_nofe /// 
				 lev_CIP_FE_nofe_preg lev_CIP_FE_nofe_fv lev_CIP_FE_nofe_fve ///
				 lev_CIP_FE_nofe_inc lev_CIP_FE_nofe_icd9 lev_CIP_FE_mco

unab indexlist3: lev_CIP_FE_W lev_CIP_FE_W_nofe

foreach var of varlist `indexlist' {
	
	keep if `var' != . 
	
	tempvar feevalpost feevalpre feevalpost2 feevalpre2
	
	by npimedicare_id YearPost: gegen `feevalpost' = mean(`var') if event_post
	by npimedicare_id YearPost: gegen `feevalpre' = mean(`var') if !event_post
	
	by npimedicare_id: gegen `feevalpost2' = mean(`feevalpost') 
	by npimedicare_id: gegen `feevalpre2' = mean(`feevalpre') 
	
	gen diffm`var' = `feevalpost2' - `feevalpre2' 
	
	drop `feevalpost' `feevalpre' `feevalpost2' `feevalpre2'	
}


*Determine # of Physicians 
bys npimedicare_id: keep if _n == 1 

//lable variables for the mover differences 
lab var diffmlev_CIP_FE "$\Delta\tau$"
lab var diffmlev_CIP_FE_W "$\Delta\tau$ (RVU weighted)"
lab var diffmlev_CIP_FE_fv "$\Delta\tau$ (first visit only)"
lab var diffmlev_CIP_FE_fve "$\Delta\tau$ (first visit excluded)"
lab var diffmlev_CIP_FE_inc "$\Delta\tau$ (local income control)"
lab var diffmlev_CIP_FE_icd9 "$\Delta\tau$ (ICD9 codes only)"
lab var diffmlev_CIP_FE_preg "$\Delta\tau$ (pregnant patients only)"

lab var diffmlev_CIP_FE_W_nofe "$\Delta\tau$ (RVU weighted)"
lab var diffmlev_CIP_FE_nofe "$\Delta\tau$"
lab var diffmlev_CIP_FE_nofe_fv "$\Delta\tau$ (first visit only)"
lab var diffmlev_CIP_FE_nofe_fve "$\Delta\tau$ (first visit excluded)"
lab var diffmlev_CIP_FE_nofe_inc "$\Delta\tau$ (local income control)"
lab var diffmlev_CIP_FE_nofe_icd9 "$\Delta\tau$ (ICD9 codes only)"
lab var diffmlev_CIP_FE_nofe_preg "$\Delta\tau$ (pregnant patients only)"

lab var diffmpi_c_FE_cpt_line "$\Delta \log(\pi(\ell)$"
lab var diffmpi_FE_cpt_line "$\Delta \log(\pi(\ell))$ (including imputations)"
lab var diffmpi_c_W_FE_cpt_line "$\Delta \log(\pi(\ell))$ (RVU weighted)"
lab var diffmpi_c_FE_cpt_line_pcp "$\Delta \log(\pi(\ell))$ (PCP specialty specific)"

lab var diffmv_initial_den_CIP "$\Delta$ Denial Rate Index"


lab var lev_CIP_FE "$\tau$"
lab var lev_CIP_FE_W "$\tau$ (RVU weighted)"
lab var lev_CIP_FE_fv "$\tau$ (first visit only)"
lab var lev_CIP_FE_fve "$\tau$ (first visit excluded)"
lab var lev_CIP_FE_inc "$\tau$ (local income control)"
lab var lev_CIP_FE_icd9 "$\tau$ (ICD9 codes only)"
lab var lev_CIP_FE_preg "$\tau$ (pregnant patients only)"
lab var lev_CIP_FE_nofe "$\tau$"
lab var lev_CIP_FE_W_nofe "$\tau$ (RVU weighted)"
lab var lev_CIP_FE_nofe_fv "$\tau$ (first visit only)"
lab var lev_CIP_FE_nofe_fve "$\tau$ (first visit excluded)"
lab var lev_CIP_FE_nofe_inc "$\tau$ (local income control)"
lab var lev_CIP_FE_nofe_icd9 "$\tau$ (ICD9 codes only)"
lab var lev_CIP_FE_nofe_preg "$\tau$ (pregnant patients only)"
lab var CIP_abs "CIP"
lab var CIP_abs_nofe "CIP"

lab var v_initial_den_CIP "Denial Rate Index"

lab var pi_c_FE_cpt_line "$\log \pi_{\ell}$"
lab var pi_FE_cpt_line "$\log \pi_{\ell}$ (including imputations)"
lab var pi_c_W_FE_cpt_line "$\log \pi_{\ell}$ (RVU weighted)"
lab var pi_c_FE_cpt_line_pcp "$\log \pi_{\ell}$ (PCP specialty specific)"
lab var pi_c_FE_cpt_line_abs "$\pi_{\ell}$"


cd `"$output/online_appendix/index_summary"'
estpost tabstat diffmpi_c_FE_cpt_line diffmpi_c_FE_cpt_line_pcp diffmpi_FE_cpt_line ///
			    diffmpi_c_W_FE_cpt_line, stat(mean sd p10 p50 p90 n) columns(statistics) 

#delimit ;
		esttab using `"diffmfee_index.tex"', cells("mean(fmt(3)) sd(fmt(2)) p10(fmt(2)) 
													p50(fmt(2)) p90(fmt(2)) count(fmt(0))") 
		unstack nonumbers replace substitute(\_ _) label booktabs nomtitles
	    noobs collabels("Mean" "SD" "10th Percentile" "50th Percentile" 
						"90th Percentile" "Observations");
#delimit cr 



estpost tabstat diffmlev_CIP_FE diffmlev_CIP_FE_W diffmlev_CIP_FE_fv ///
				diffmlev_CIP_FE_fve diffmlev_CIP_FE_inc diffmlev_CIP_FE_icd9 ///
				diffmlev_CIP_FE_preg diffmv_initial_den_CIP , ///
				stat(mean sd p10 p50 p90 n) columns(statistics) 

#delimit ;
		esttab using `"diffmcip_index_FE.tex"', cells("mean(fmt(3)) sd(fmt(2)) p10(fmt(2)) 
													   p50(fmt(2)) p90(fmt(2)) count(fmt(0))") 
		unstack nonumbers replace substitute(_ \_) label booktabs nomtitles
	    noobs collabels("Mean" "SD" "10th Percentile" "50th Percentile" 
						"90th Percentile" "Observations");
#delimit cr 

estpost tabstat  diffmlev_CIP_FE_nofe diffmlev_CIP_FE_W_nofe diffmlev_CIP_FE_nofe_fv ///
				 diffmlev_CIP_FE_nofe_fve diffmlev_CIP_FE_nofe_inc ///
				 diffmlev_CIP_FE_nofe_icd9 diffmlev_CIP_FE_nofe_preg , ///
				 stat(mean sd p10 p50 p90 n) columns(statistics) 

#delimit ;
		esttab using `"diffmcip_index_HC.tex"', cells("mean(fmt(3)) sd(fmt(2)) p10(fmt(2)) 
													   p50(fmt(2)) p90(fmt(2)) count(fmt(0))") 
		unstack nonumbers replace substitute(_ \_) label booktabs nomtitles
	    noobs collabels("Mean" "SD" "10th Percentile" "50th Percentile" 
						"90th Percentile" "Observations");
#delimit cr 



eststo clear 
bys payee_state_cd: keep if _n == 1 

//normalize indices 

//unweighted indices
//sum mean_tau, d 
//local tau_mean `r(mean)'

foreach var of varlist `indexlist2' {
	
	sum `var' 
	local nconst = 0.176 - r(mean)
	
	replace `var' = `var' + `nconst'
	sum `var'
}

//weighted indices
foreach var of varlist `indexlist3' {
	
	sum `var' 
	local nconst = 0.131 - r(mean)
	
	replace `var' = `var' + `nconst'
	sum `var'
}

//denial rate index
sum mean_denial_rate, d 
local denial_rate_mean `r(mean)'
foreach var of varlist v_initial_den_CIP {
	
	sum `var' 
	local nconst = `denial_rate_mean' - r(mean)
	
	replace `var' = `var' + `nconst'
	sum `var'
}

//CIP in levels
sum mean_CIP, d 
local CIP_mean `r(mean)'
foreach var of varlist CIP_abs_nofe CIP_abs {
	
	sum `var' 
	local nconst = `CIP_mean' - r(mean)
	
	replace `var' = `var' + `nconst'
	sum `var'
}


estpost tabstat pi_c_FE_cpt_line pi_c_FE_cpt_line_pcp pi_FE_cpt_line ///
				pi_c_W_FE_cpt_line pi_c_FE_cpt_line_abs, ///
				stat(mean sd p10 p50 p90 n) columns(statistics) 

#delimit ;
		esttab using `"fee_index.tex"', cells("mean(fmt(2)) sd(fmt(2)) p10(fmt(2)) 
											   p50(fmt(2)) p90(fmt(2)) count(fmt(0))") 
		unstack nonumbers replace substitute(\_ _) label booktabs nomtitles
	    noobs collabels("Mean" "SD" "10th Percentile" "50th Percentile" 
						"90th Percentile" "Observations");
#delimit cr 



estpost tabstat lev_CIP_FE lev_CIP_FE_W lev_CIP_FE_fv lev_CIP_FE_fve ///
				lev_CIP_FE_inc lev_CIP_FE_icd9 lev_CIP_FE_preg v_initial_den_CIP ///
				CIP_abs, stat(mean sd p10 p50 p90 n) columns(statistics) 

#delimit ;
		esttab using `"cip_index_FE.tex"', cells("mean(fmt(2)) sd(fmt(2)) p10(fmt(2)) 
												  p50(fmt(2)) p90(fmt(2)) count(fmt(0))") 
		unstack nonumbers replace substitute(_ \_) label booktabs nomtitles
	    noobs collabels("Mean" "SD" "10th Percentile" "50th Percentile" 
						"90th Percentile" "Observations");
#delimit cr 

estpost tabstat lev_CIP_FE_nofe lev_CIP_FE_W_nofe lev_CIP_FE_nofe_fv ///
				lev_CIP_FE_nofe_fve lev_CIP_FE_nofe_inc lev_CIP_FE_nofe_icd9 /// 
				lev_CIP_FE_nofe_preg CIP_abs_nofe, ///
				stat(mean sd p10 p50 p90 n) columns(statistics) 

#delimit ;
		esttab using `"cip_index_HC.tex"', cells("mean(fmt(2)) sd(fmt(2)) p10(fmt(2))
												  p50(fmt(2)) p90(fmt(2)) count(fmt(0))") 
		unstack nonumbers replace substitute(_ \_) label booktabs nomtitles
	    noobs collabels("Mean" "SD" "10th Percentile" "50th Percentile" 
						"90th Percentile" "Observations");
#delimit cr 




//Combine the tables into one tables with three panels 
cap prog drop panelcombine
prog define panelcombine
qui {
syntax, use(str asis) paneltitles(str asis) columncount(integer) save(str asis) [CLEANup]
preserve

tokenize `"`paneltitles'"'
//read in loop
local num 1
while "``num''"~="" {
local panel`num'title="``num''"
local num=`num'+1
}


tokenize `use'
//read in loop
local num 1
while "``num''"~="" {
tempfile temp`num'
insheet using "``num''", clear
save `temp`num''
local max = `num'
local num=`num'+1
}

//conditional processing loop
local num 1
while "``num''"~="" {
local panellabel : word `num' of `c(alpha)'
use `temp`num'', clear
	if `num'==1 { //process first panel -- clip bottom
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}"
	replace v1 = "\midrule  \multicolumn{`columncount'}{c}{\textbf{Panel `panellabel': `panel1title'}}  \vspace{3pt} \\" if v1=="\midrule" & _n<8
	replace v1 = "" if v1=="\bottomrule" & _n>4 //this is intended to replace the bottom double line; more robust condition probably exists
	}
	else if `num'==`max' { //process final panel -- clip top
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	replace v1 = " \multicolumn{`columncount'}{c}{\textbf{Panel `panellabel': `panel`num'title'}} \vspace{3pt} \\" if _n==1
	}
	else { //process middle panels -- clip top and bottom
	//process header to drop everything until first hline
	g temp = (v1 == "\midrule")
	replace temp = temp+temp[_n-1] if _n>1
	drop if temp==0
	drop temp
	
	replace v1 = " \multicolumn{`columncount'}{c}{\textbf{Panel `panellabel': `panel`num'title'}} \vspace{3pt} \\" if _n==1
	drop if strpos(v1,"Note:")>0 | strpos(v1,"in parentheses")>0 | strpos(v1,"p<0")>0
	drop if v1=="\end{tabular}" | v1=="}"
	replace v1 = "" if v1=="\bottomrule"
	}
	save `temp`num'', replace
local num=`num'+1
}

use `temp1',clear
local num 2
while "``num''"~="" {
append using `temp`num''
local num=`num'+1
}

outsheet using `save', noname replace noquote


	if "`cleanup'"!="" { //erasure loop
	tokenize `use'
	local num 1
		while "``num''"~="" {
		erase "``num''"
		local num=`num'+1
		}
	}

restore
}
end


panelcombine, use(fee_index.tex cip_index_FE.tex cip_index_HC.tex)  ///
	columncount(7) paneltitles("$\pi$ Indices" ///
							   "$\tau$ and CIP Indices (Physician Fixed Effects)" ///
							   "$\tau$ and CIP Indices (Heckmann Correction)") ///
	save(`"$online_appendix/summary_indices.tex"') cleanup 

panelcombine, use(diffmfee_index.tex diffmcip_index_FE.tex diffmcip_index_HC.tex) ///
	columncount(7) paneltitles("$\Delta \log \pi$ Index" ///
							   "$\Delta \tau$ Index (Physician Fixed Effects)" ///
							   "$\Delta \tau$ Index (Heckmann Correction)") ///
	save(`"$online_appendix/summary_indices_diffmove.tex"') cleanup 


